
/**
 * https://blog.csdn.net/wanglei19880622/article/details/114387077?ops_request_misc=&request_id=&biz_id=102&utm_term=node.js%20%E8%A7%A3%E5%8E%8B%E5%B8%A6%E5%AF%86%E7%A0%81&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~sobaiduweb~default-0-114387077.nonecase&spm=1018.2226.3001.4450
 */


/**
 * @todo 加入到配置文件中
 * command: 命令
 * zip：要解压的文件名
 * bfl: 多线程线程数
 * 
 */
const exec = require('child_process').exec
/**
 * 
 * @param {*} cmdStr 
 * @returns 
 */
function unzipCmd(cmdStr) {
// function unzipCmd(cmdPath, zipPath, outPath, pwd) {
    /**
     * winrar下载地址 https://www.winrar.com.cn/
    */
    // let cmdStr1 = 'WinRAR.exe -ibck -y x -t -p' + pwd + ' "' + zipPath + '"  "' + outPath + '"'; // WinRAR.exe
    // let cmdStr1 = '"' + 'D:\\Program Files (x86)\\WinRAR\\WinRAR.exe' + '"' + ' -ibck -y x -t -p' + pwd + ' "' + zipPath + '"  "' + outPath + '"'; // WinRAR.exe

    let cmdStr1 = cmdStr; // WinRAR.exe

    // let cmdStr1 = '360zip.exe -X ' + zipPath + ' '  + outPath; // 360zip.exe ; !!!360暂不支持 带密码的命令行参数
    //let cmdPath = cmdPath
    // 子进程名称
    // console.log(cmdStr1);
    let workerProcess;
    return runExec(cmdStr1)
    function runExec(cmdStr) {
        return new Promise((_resolve, _reject) => {
            // workerProcess = exec(cmdStr, { cwd: cmdPath });
            workerProcess = exec(cmdStr, { cwd: process.cwd() });
            // 打印正常的后台可执行程序输出
            workerProcess.stdout.on('data', function (data) {
                // console.log('stdout: ' + data);
            })
            // 打印错误的后台可执行程序输出
            workerProcess.stderr.on('data', function (data) {
                // console.log('stderr: ' + data);
                _reject();
            })
            // 退出之后的输出
            workerProcess.on('close', function (code) {
                // console.log('out code：' + code);
                _resolve(code);
            })
        });
    }
}

module.exports = {
    "unzipCmd": unzipCmd
};